package com.zlp.mall.common.util;

import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;

/** 
 * 脱敏工具类
 * @date: 2021/9/29 9:48
 */
public class DesensitizationUtils {


    /**
     * 姓名脱敏
     * 规则: 保留姓 (张**)
     *
     * @param fullName
     * @date: 2021/9/29 9:19
     * @return: java.lang.String
     */
    private static String desensitizedName(String fullName){
        if (!Strings.isNullOrEmpty(fullName)) {
            String name = StringUtils.left(fullName, 1);
            return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
        }
        return fullName;
    }

    /**
     * 名字脱敏
     * 规则，张三丰，脱敏为：张*丰
     *
     * @param name
     * @return
     */
    public static String nameDesensitization(String name) {
        if (name == null || name.isEmpty()) {
            return "";
        }
        String myName = null;
        char[] chars = name.toCharArray();
        if (chars.length == 1) {
            myName = name;
        }
        if (chars.length == 2) {
            myName = name.replaceFirst(name.substring(1), "*");
        }
        if (chars.length > 2) {
            myName = name.replaceAll(name.substring(1, chars.length - 1), "*");
        }
        return myName;
    }

    /**
     * 手机号码脱敏
     *  脱敏规则: 保留前三后四, 比如 15566026528 置换为 155****6528
     *
     * @param phoneNumber
     * @date: 2021/9/29 9:19
     * @return: java.lang.String
     */
    private static String desensitizedPhoneNumber(String phoneNumber){
        if(StringUtils.isNotEmpty(phoneNumber)){
            phoneNumber = phoneNumber.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2");
        }
        return phoneNumber;
    }

    /**
     * 身份证号
     * 规则：保留前六后三 (462329*********935)
     *
     * @param idNumber
     * @date: 2021/9/29 9:19
     * @return: java.lang.String
     */
    private static String desensitizedIdNumber(String idNumber){

        if (!Strings.isNullOrEmpty(idNumber)) {
            if (idNumber.length() == 15){
                idNumber = idNumber.replaceAll("(\\w{6})\\w*(\\w{3})", "$1******$2");
            }
            if (idNumber.length() == 18){
                idNumber = idNumber.replaceAll("(\\w{6})\\w*(\\w{3})", "$1*********$2");
            }
        }
        return idNumber;
    }

    /**
     * 地址
     * 脱敏规则: 从第4位开始隐藏,隐藏8位
     *
     * @param address
     * @date: 2021/9/29 9:19
     * @return: java.lang.String
     */
    private static String desensitizedAddress(String address){

        if (!Strings.isNullOrEmpty(address)) {
            return StringUtils.left(address, 3).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(address, address.length()-11), StringUtils.length(address), "*"), "***"));
        }
        return address;
    }

}
